package com.et.machine.manager.vm.setting;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;

import com.et.drive.info.Time;
import com.et.machine.R;
import com.et.machine.databinding.DialogSettingTimeBinding;

public class SetTimeDialog extends Dialog {
    private OnDialogListener customDialogListener;
    private DialogSettingTimeBinding binding;
    private Time time;


    public SetTimeDialog(Context context, Time time, OnDialogListener customDialogListener) {
        super(context, R.style.common_dialog);
        this.customDialogListener = customDialogListener;
        this.time = time;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogSettingTimeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.startTimepicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        binding.startTimepicker.setIs24HourView(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            binding.startTimepicker.setHour(time.getStartHour());
            binding.startTimepicker.setMinute(time.getStartMin());
        } else {

        }

        binding.startTimepicker.setOnTimeChangedListener((view, hourOfDay, minute) -> {
            time.setStartHour(hourOfDay);
            time.setStartMin(minute);
        });

        binding.endTimepicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        binding.endTimepicker.setIs24HourView(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            binding.endTimepicker.setHour(time.getEndHour());
            binding.endTimepicker.setMinute(time.getEndMin());
        }

        binding.endTimepicker.setOnTimeChangedListener((view, hourOfDay, minute) -> {
            time.setEndHour(hourOfDay);
            time.setEndMin(minute);
        });

        binding.ensure.setOnClickListener(ensure_clickListener);
        binding.cancel.setOnClickListener(cancel_clickListener);
    }

    //发送消息,回调函数取值;
    private View.OnClickListener ensure_clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customDialogListener.back(time);
            SetTimeDialog.this.dismiss();
        }
    };

    //取消发送;
    private View.OnClickListener cancel_clickListener = v -> {
        SetTimeDialog.this.dismiss();
    };

    //定义回调事件，用于dialog的点击后的处理事件
    public interface OnDialogListener {
        public void back(Time time);
    }

}
